// ExceptionExample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

const int MYMAX = 7;

template <class T>
T average(const T* p, int len)
{
	if(len <= 0)
	{
		throw"Average: array length is not greater than zero";
	}

	if(len > MYMAX)
	{
		throw"Average: array length is  greater than max";
	}

	T s = 0;

	for(int i = 0;i < len; i++)
	{
		s += p[i];
	}

	return s/len;
}


double week_average(const double* p, int len)
{
	try
	{
		return average(p,len);
	}
	catch(char* p_msg)
	{
		cerr<<"week_average: length must be between 1 and max"<<endl;
		throw;
	}
}


double call_week_average(const double* p,int n)
{
	try
	{
		return week_average(p,n);
	}
	catch(char* p_msg)
	{
		cerr<<p_msg<<endl;
		cout<<"Input another length:";
		cin>>n;

		return call_week_average(p,n);
	}
}


int _tmain()
{
	int n = 10;
	double week[7] = {1.2,2.2,3.3,4.4,5.5,6.6,7.7};

	cout<<"The average is"<<call_week_average(week,n)<<endl;
	return 0;
}

